package com.ruoyi.project.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.SysAreaMapper;
import com.ruoyi.project.system.domain.SysArea;
import com.ruoyi.project.system.service.ISysAreaService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author theme
 * @date 2020-06-24
 */
@Service
public class SysAreaServiceImpl implements ISysAreaService {
    @Autowired
    private SysAreaMapper sysAreaMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param areaId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public SysArea selectSysAreaById(Long areaId) {
        return sysAreaMapper.selectSysAreaById(areaId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysArea 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysArea> selectSysAreaList(SysArea sysArea) {
        return sysAreaMapper.selectSysAreaList(sysArea);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysArea 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysArea(SysArea sysArea) {
        return sysAreaMapper.insertSysArea(sysArea);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysArea 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysArea(SysArea sysArea) {
        return sysAreaMapper.updateSysArea(sysArea);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param areaIds 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteSysAreaByIds(Long[] areaIds) {
        return sysAreaMapper.deleteSysAreaByIds(areaIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param areaId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteSysAreaById(Long areaId) {
        return sysAreaMapper.deleteSysAreaById(areaId);
    }

    @Override
    public List<SysArea> buildAreaTree(List<SysArea> areas) {
        List<Long> areaIds = new ArrayList<>();
        for (SysArea sysArea : areas) {
            areaIds.add(sysArea.getAreaId());
        }

        List<Long> parentIds = new ArrayList<>();
        for (SysArea sysArea : areas) {
            if (!areaIds.contains(sysArea.getPid())) {
                parentIds.add(sysArea.getAreaId());
            }
        }
        List<SysArea> returnList = new ArrayList<SysArea>();
        for (Iterator<SysArea> iterator = areas.iterator(); iterator.hasNext(); ) {
            SysArea t = (SysArea) iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (parentIds.contains(t.getAreaId())) {
                recursionFn(areas, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty()) {
            returnList = areas;
        }
        return returnList;
    }

    @Override
    public List<SysArea> queryProvince() {
        return sysAreaMapper.queryProvince();
    }

    @Override
    public List<SysArea> queryCity() {
        return sysAreaMapper.queryCity();
    }

    @Override
    public List<SysArea> queryDistrict() {
        return sysAreaMapper.queryDistrict();
    }

    @Override
    public List<SysArea> queryAllData(SysArea sysArea) {
        return sysAreaMapper.queryAllData(sysArea);
    }

    @Override
    public SysArea selectCity(String city) {
        return sysAreaMapper.selectCity(city);
    }

    @Override
    public List<SysArea> queryProvinceData(SysArea sysArea) {
        return null;
    }

    @Override
    public List<SysArea> queryCityData(SysArea sysArea) {
        return null;
    }

    @Override
    public List<SysArea> queryAreaData(SysArea sysArea) {
        return null;
    }

    private void recursionFn(List<SysArea> areas, SysArea t) {
        {
            // 得到子节点列表
            List<SysArea> childList = getChildList(areas, t);
            t.setChildren(childList);
            for (SysArea tChild : childList) {
                if (hasChild(areas, tChild)) {
                    // 判断是否有子节点
                    Iterator<SysArea> it = childList.iterator();
                    while (it.hasNext()) {
                        SysArea n = (SysArea) it.next();
                        recursionFn(areas, n);
                    }
                }
            }
        }
    }
    private List<SysArea> getChildList(List<SysArea> list, SysArea t) {
        List<SysArea> tlist = new ArrayList<SysArea>();
        Iterator<SysArea> it = list.iterator();
        while (it.hasNext()) {
            SysArea n = (SysArea) it.next();
            if (n.getPid().longValue() == t.getAreaId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    private boolean hasChild(List<SysArea> areas, SysArea t) {
        return getChildList(areas, t).size() > 0 ? true : false;
    }
}
